using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ERP.Common.Results;
using ERP.ERPSystem.WarehouseManagement;
using ERP.ERPSystem.WarehouseManagement.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace ERP.Web.Controllers.WarehouseManagement
{
    /// <summary>
    /// 装箱管理控制器
    /// </summary>
    public class PackingController : Controller
    {
        private readonly IPackingServices _packingServices;
        private readonly ILogger<PackingController> _logger;

        public PackingController(IPackingServices packingServices, ILogger<PackingController> logger)
        {
            _packingServices = packingServices;
            _logger = logger;
        }

        /// <summary>
        /// 装箱管理主页面
        /// </summary>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增装箱单页面
        /// </summary>
        public IActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 编辑装箱单页面
        /// </summary>
        public IActionResult Edit(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 获取装箱管理分页列表
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetPagedList([FromBody] PackingQueryDto query)
        {
            try
            {
                if (query.PageNumber <= 0) query.PageNumber = 1;
                if (query.PageSize <= 0) query.PageSize = 10;

                var result = await _packingServices.GetPagedListAsync(query);
                return Json(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取装箱管理列表失败");
                return Json(APIResult<object>.CreateError("获取数据失败"));
            }
        }

        /// <summary>
        /// 根据ID获取装箱管理详情
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                var result = await _packingServices.GetByIdAsync(id);
                return Json(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取装箱管理详情失败");
                return Json(APIResult<object>.CreateError("获取数据失败"));
            }
        }

        /// <summary>
        /// 创建装箱管理
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] PackingCreateDto createDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(APIResult<object>.CreateError("数据验证失败"));
                }

                var result = await _packingServices.CreateAsync(createDto);
                return Json(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建装箱管理失败");
                return Json(APIResult<object>.CreateError("创建失败"));
            }
        }

        /// <summary>
        /// 更新装箱管理
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] PackingUpdateDto updateDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(APIResult<object>.CreateError("数据验证失败"));
                }

                var result = await _packingServices.UpdateAsync(updateDto);
                return Json(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新装箱管理失败");
                return Json(APIResult<object>.CreateError("更新失败"));
            }
        }

        /// <summary>
        /// 删除装箱管理
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                var result = await _packingServices.DeleteAsync(id);
                return Json(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除装箱管理失败");
                return Json(OperationResult.ErrorResult("删除失败"));
            }
        }

        /// <summary>
        /// 批量删除装箱管理
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> BatchDelete([FromBody] List<int> ids)
        {
            try
            {
                var result = await _packingServices.BatchDeleteAsync(ids);
                return Json(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "批量删除装箱管理失败");
                return Json(OperationResult.ErrorResult("批量删除失败"));
            }
        }
    }
} 